package com.myk.game.gobangchess.ai.shock;

public class Checkifcheck {

    public static int checkifcheck(int[][] board, int playerTurn, int x, int y) {
        int ifCheck = 1;

        if (x == 0 || x == 7) {
            int xCheck = (x == 0) ? 1 : 6;

            if (y >= 2 && y <= 5) {
                if (board[y][xCheck] != playerTurn && board[y + 1][xCheck] != playerTurn && board[y - 1][xCheck] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (y == 6) {
                if (board[y][xCheck] != playerTurn && board[y - 1][xCheck] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (y == 1) {
                if (board[y][xCheck] != playerTurn && board[y + 1][xCheck] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (y == 7) {
                if (board[y - 1][xCheck] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (y == 0) {
                if (board[y + 1][xCheck] != playerTurn) {
                    ifCheck = 0;
                }
            }
        }

        if (y == 0 || y == 7) {
            int yCheck = (y == 0) ? 1 : 6;

            if (x >= 2 && x <= 5) {
                if (board[yCheck][x] != playerTurn && board[yCheck][x + 1] != playerTurn && board[yCheck][x - 1] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (x == 6) {
                if (board[yCheck][x] != playerTurn && board[yCheck][x - 1] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (x == 1) {
                if (board[yCheck][x] != playerTurn && board[yCheck][x + 1] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (x == 7) {
                if (board[yCheck][x - 1] != playerTurn) {
                    ifCheck = 0;
                }
            }

            if (x == 0) {
                if (board[yCheck][x + 1] != playerTurn) {
                    ifCheck = 0;
                }
            }
        }

        return ifCheck;
    }
}

